package com.zr.dao.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.ReprotDao;
import com.zr.datamodel.GoodsDM;
import com.zr.datamodel.OrderDM;
import com.zr.datamodel.RcdDeliveryDM;
import com.zr.datamodel.RcdReceivingDM;
import com.zr.dto.KeysQueryDto;
import com.zr.dto.RedReceivingDM;

/**
 * 报表DAO层
 * @author huzhou
 *
 * @param <T>
 */
@Repository("recReprotDao")
public class RecReprotDaoImpl<T> extends BaseDaOImpl<T> implements ReprotDao<T>{
    
	/**
	 * 收货报表dao
	 */
	@Override
	public List<RedReceivingDM> selectReprot(String year, String[] month, int page, int rows) {
		// TODO Auto-generated method stub
		String hql = " from RcdReceivingDM r ";
		String wherehql = hql;
		Query  query ;
		if (null != year && month[0]==null && month[1]==null && month[2] == null) {
			wherehql = hql+" where r.receivingDate like '%" +year+"%'";
		}else if (null != year &&  month[0] !=null) {
			wherehql = hql+" where r.receivingDate like '"+year+"-"+month[0]+"-%' or r.receivingDate like '"+year+"-"+month[1]+"-%' or r.receivingDate like '"+year+"-"+month[2]+"-%'";
		}
		query  =  this.getCurrentSession().createQuery(wherehql);
		System.out.println(wherehql);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<RcdReceivingDM> rcds = query.list();
		List<OrderDM> oders = new ArrayList<OrderDM>();
		List<RedReceivingDM> rec = new ArrayList<RedReceivingDM>();
		for (RcdReceivingDM rcdReceivingDM : rcds) {
					oders = rcdReceivingDM.getOrder();
					
			for (OrderDM orderDM : oders) {
				RedReceivingDM red = new RedReceivingDM();
				//获取当前时间 设为报表生成时间
				Calendar cal=Calendar.getInstance();
				String y=String.valueOf(cal.get(Calendar.YEAR));
				String m=String.valueOf(cal.get(Calendar.MONTH)+1); //获取当前月是要+1
				String d=String.valueOf(cal.get(Calendar.DATE));
				String ctime = y+"-"+m+"-"+d;
				
				red.setCtime(ctime);
				red.setSname(orderDM.getEndPlace());
				red.setFname(orderDM.getStartPlace());
				red.setPtime(orderDM.getSendData());
				red.setIncome(orderDM.getConutMoney());
				
				//获取订单中的货物
				List<GoodsDM> goods = orderDM.getGoods();
				int weightsum = 0;
				int sizesum = 0;
					for (GoodsDM goodsDM : goods) {
						weightsum += goodsDM.getWeight(); //每个订单货物总重量
						sizesum += goodsDM.getSize(); //每个订单货物总体积
					}
				red.setWeight(weightsum);
				red.setSize(sizesum);
				rec.add(red);
			}
		}
		return rec;    		
	}
    
	/**
	 * 收货报表数量dao
	 */
	@Override
	public long findReprotMsgCount(String year,String[] month) {
		String hql = " select count(*) from RcdReceivingDM r";
		String wherehql = hql;
		if (null != year && month[0]==null && month[1]==null && month[2] == null) {
			wherehql = hql+" where r.receivingDate like '%" +year+"%'";
		}
		if (null != year && month[0] !=null) {
			wherehql = hql+" where r.receivingDate like '"+year+"-"+month[0]+"-%' or r.receivingDate like '"+year+"-"+month[1]+"-%' or r.receivingDate like '"+year+"-"+month[2]+"-%'";
		}
		List<Object>  obj = this.getCurrentSession().createQuery(wherehql).list();
		return (long) obj.get(0);
	}
    
	/**
	 * 发货报表dao
	 */
	@Override
	public List<RedReceivingDM> selectdelReprot(String year, String[] month, int page, int rows) {
		// TODO Auto-generated method stub
		String hql = " from RcdDeliveryDM r ";
		String wherehql = hql;
		Query  query ;
		if (null != year && month[0]==null && month[1]==null && month[2] == null) {
			wherehql = hql+" where r.deliveryDate like '%" +year+"%'";
		}else if (null != year &&  month[0] !=null) {
			wherehql = hql+" where r.deliveryDate like '"+year+"-"+month[0]+"-%' or r.deliveryDate like '"+year+"-"+month[1]+"-%' or r.deliveryDate like '"+year+"-"+month[2]+"-%'";
		}
		query  =  this.getCurrentSession().createQuery(wherehql);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<RcdDeliveryDM> rcds = query.list();
		List<OrderDM> oders = new ArrayList<OrderDM>();
		List<RedReceivingDM> rec = new ArrayList<RedReceivingDM>();
		for (RcdDeliveryDM rcdReceivingDM : rcds) {
					oders = rcdReceivingDM.getOrder();
					
			for (OrderDM orderDM : oders) {
				RedReceivingDM red = new RedReceivingDM();
				//获取当前时间 设为报表生成时间
				Calendar cal=Calendar.getInstance();
				String y=String.valueOf(cal.get(Calendar.YEAR));
				String m=String.valueOf(cal.get(Calendar.MONTH)+1); //获取当前月是要+1
				String d=String.valueOf(cal.get(Calendar.DATE));
				String ctime = y+"-"+m+"-"+d;
				
				red.setCtime(ctime);
				red.setSname(orderDM.getEndPlace());
				red.setFname(orderDM.getStartPlace());
				red.setPtime(orderDM.getSendData());
				red.setIncome(orderDM.getConutMoney());
				
				//获取订单中的货物
				List<GoodsDM> goods = orderDM.getGoods();
				int weightsum = 0;
				int sizesum = 0;
					for (GoodsDM goodsDM : goods) {
						weightsum += goodsDM.getWeight(); //每个订单货物总重量
						sizesum += goodsDM.getSize(); //每个订单货物总体积
					}
				red.setWeight(weightsum);
				red.setSize(sizesum);
				rec.add(red);
			}
		}
		return rec;  
	}
    
	/**
	 * 发货报表数量dao
	 */
	@Override
	public long finddelReprotMsgCount(String year, String[] month) {
		// TODO Auto-generated method stub
		String hql = " select count(*) from RcdDeliveryDM r";
		String wherehql = hql;
		if (null != year && month[0] == null && month[1]== null && month[2] == null) {
			wherehql = hql+" where r.deliveryDate like '%" +year+"%'";
		}
		if (null != year  && month[0] !=null) {
			wherehql = hql+" where r.deliveryDate like '"+year+"-"+month[0]+"-%' or r.deliveryDate like '"+year+"-"+month[1]+"-%' or r.deliveryDate like '"+year+"-"+month[2]+"-%'";
		}
		System.out.println(wherehql);
		List<Object>  obj = this.getCurrentSession().createQuery(wherehql).list();
		return (long) obj.get(0);
	}

	@Override
	public List<RedReceivingDM> selectRDReprot(String year, String[] month, int page, int rows) {
		// TODO Auto-generated method stub
		String hql = " from RcdDeliveryDM r ";
		String wherehql = hql;
		Query  query ;
		if (null != year && month[0]==null && month[1]==null && month[2] == null) {
			wherehql = hql+" where r.deliveryDate like '%" +year+"%'";
		}else if (null != year &&  month[0] !=null) {
			wherehql = hql+" where r.deliveryDate like '"+year+"-"+month[0]+"-%' or r.deliveryDate like '"+year+"-"+month[1]+"-%' or r.deliveryDate like '"+year+"-"+month[2]+"-%'";
		}
		query  =  this.getCurrentSession().createQuery(wherehql);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<RcdDeliveryDM> rcds = query.list();
		List<OrderDM> oders = new ArrayList<OrderDM>();
		List<RedReceivingDM> rec = new ArrayList<RedReceivingDM>();
		for (RcdDeliveryDM rcdReceivingDM : rcds) {
					oders = rcdReceivingDM.getOrder();
					
			for (OrderDM orderDM : oders) {
				RedReceivingDM red = new RedReceivingDM();
				//获取当前时间 设为报表生成时间
				Calendar cal=Calendar.getInstance();
				String y=String.valueOf(cal.get(Calendar.YEAR));
				String m=String.valueOf(cal.get(Calendar.MONTH)+1); //获取当前月是要+1
				String d=String.valueOf(cal.get(Calendar.DATE));
				String ctime = y+"-"+m+"-"+d;
				
				red.setCtime(ctime);
				red.setSname(orderDM.getEndPlace());
				red.setFname(orderDM.getStartPlace());
				red.setPtime(orderDM.getSendData());
				red.setIncome(orderDM.getConutMoney());
				
				//获取订单中的货物
				List<GoodsDM> goods = orderDM.getGoods();
				int weightsum = 0;
				int sizesum = 0;
					for (GoodsDM goodsDM : goods) {
						weightsum += goodsDM.getWeight(); //每个订单货物总重量
						sizesum += goodsDM.getSize(); //每个订单货物总体积
					}
				red.setWeight(weightsum);
				red.setSize(sizesum);
				rec.add(red);
			}
		}
		return rec;  
	}

	@Override
	public long findRDReprotMsgCount(String year, String[] month) {
		// TODO Auto-generated method stub
		String hql = " select count(*) from RcdDeliveryDM r";
		String wherehql = hql;
		if (null != year && month[0] == null && month[1]== null && month[2] == null) {
			wherehql = hql+" where r.deliveryDate like '%" +year+"%'";
		}
		if (null != year  && month[0] !=null) {
			wherehql = hql+" where r.deliveryDate like '"+year+"-"+month[0]+"-%' or r.deliveryDate like '"+year+"-"+month[1]+"-%' or r.deliveryDate like '"+year+"-"+month[2]+"-%'";
		}
		System.out.println(wherehql);
		List<Object>  obj = this.getCurrentSession().createQuery(wherehql).list();
		return (long) obj.get(0);
	}

	@Override
	public List<RedReceivingDM> selectGReprot(String year, String[] month, int page, int rows) {
		// TODO Auto-generated method stub
		String hql = " from OrderDM o ";
		String wherehql = hql;
		Query  query ;
		if (null != year && month[0]==null && month[1]==null && month[2] == null) {
			wherehql = hql+" where o.sendData like '%" +year+"%'";
		}else if (null != year &&  month[0] !=null) {
			wherehql = hql+" where o.sendData like '"+year+"-"+month[0]+"-%' or o.sendData like '"+year+"-"+month[1]+"-%' or o.sendData like '"+year+"-"+month[2]+"-%'";
		}
		System.out.println(wherehql);
		query  =  this.getCurrentSession().createQuery(wherehql);
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);
		List<OrderDM> orders = query.list();
		List<RedReceivingDM> rec = new ArrayList<RedReceivingDM>();
		for (OrderDM orderDM : orders) {
			RedReceivingDM red = new RedReceivingDM();
			//获取当前时间 设为报表生成时间
			Calendar cal=Calendar.getInstance();
			String y=String.valueOf(cal.get(Calendar.YEAR));
			String m=String.valueOf(cal.get(Calendar.MONTH)+1); //获取当前月是要+1
			String d=String.valueOf(cal.get(Calendar.DATE));
			String ctime = y+"-"+m+"-"+d;
			
			red.setCtime(ctime);
			red.setSname(orderDM.getEndPlace());
			red.setFname(orderDM.getStartPlace());
			red.setPtime(orderDM.getSendData());
			red.setIncome(orderDM.getConutMoney());
			
			//获取订单中的货物
			List<GoodsDM> goods = orderDM.getGoods();
			int weightsum = 0;
			int sizesum = 0;
				for (GoodsDM goodsDM : goods) {
					weightsum += goodsDM.getWeight(); //每个订单货物总重量
					sizesum += goodsDM.getSize(); //每个订单货物总体积
				}
			red.setWeight(weightsum);
			red.setSize(sizesum);
			rec.add(red);
		}
		
		return rec;    		
	}

	@Override
	public long findGReprotMsgCount(String year, String[] month) {
		// TODO Auto-generated method stub
		String hql = " select count(*) from OrderDM o";
		String wherehql = hql;
		if (null != year && month[0]==null && month[1]==null && month[2] == null) {
			wherehql = hql+" where o.sendData like '%" +year+"%'";
		}
		if (null != year && month[0] !=null) {
			wherehql = hql+" where o.sendData like '"+year+"-"+month[0]+"-%' or o.sendData like '"+year+"-"+month[1]+"-%' or o.sendData like '"+year+"-"+month[2]+"-%'";
		}
		List<Object>  obj = this.getCurrentSession().createQuery(wherehql).list();
		return (long) obj.get(0);
	}
     
}
